//
// Created by ChenJi on 2021/4/4.
//

#include "string_library.h"

char const itoa_index[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

uint8_t string_buf[30];

uint8_t myitoa(int32_t num, uint8_t *str, uint8_t radix) {
    unsigned unum;
    uint8_t i = 0, j, k;
    if (radix == 10 && num < 0) {
        unum = (unsigned) -num;
        str[i++] = '-';
    } else unum = (unsigned) num;
    do {
        str[i++] = itoa_index[unum % (unsigned) radix];
        unum /= radix;
    } while (unum);
    str[i] = '\0';
    if (str[0] == '-') k = 1;
    else k = 0;
    char temp;
    for (j = k; j <= (i - k - 1) / 2.0; j++) {
        temp = str[j];
        str[j] = str[i - j - 1];
        str[i - j - 1] = temp;
    }
    return i;
}

uint8_t mystr_len(uint8_t *data) {
    uint8_t i = 0;
    while ((*data++) != '\0')
        i++;
    return i;
}

uint8_t *
format_time(uint8_t year, uint8_t day, uint8_t month, uint8_t hour, uint8_t minute, uint8_t second, uint8_t *buf) {
    uint8_t index = 0;
    uint8_t len = 0;
    myitoa(year, string_buf, 10);
    *(buf + index++) = string_buf[0];
    *(buf + index++) = string_buf[1];
    *(buf + index++) = '/';
    len = myitoa(month, string_buf, 10);
    if (len == 1) {
        *(buf + index++) = '0';
        *(buf + index++) = string_buf[0];
    } else {
        *(buf + index++) = string_buf[0];
        *(buf + index++) = string_buf[1];
    }
    *(buf + index++) = '/';
    len = myitoa(day, string_buf, 10);
    if (len == 1) {
        *(buf + index++) = '0';
        *(buf + index++) = string_buf[0];
    } else {
        *(buf + index++) = string_buf[0];
        *(buf + index++) = string_buf[1];
    }
    *(buf + index++) = ' ';
    len = myitoa(hour, string_buf, 10);
    if (len == 1) {
        *(buf + index++) = '0';
        *(buf + index++) = string_buf[0];
    } else {
        *(buf + index++) = string_buf[0];
        *(buf + index++) = string_buf[1];
    }
    *(buf + index++) = ':';
    len = myitoa(minute, string_buf, 10);
    if (len == 1) {
        *(buf + index++) = '0';
        *(buf + index++) = string_buf[0];
    } else {
        *(buf + index++) = string_buf[0];
        *(buf + index++) = string_buf[1];
    }
    *(buf + index++) = ':';
    len = myitoa(second, string_buf, 10);
    if (len == 1) {
        *(buf + index++) = '0';
        *(buf + index++) = string_buf[0];
    } else {
        *(buf + index++) = string_buf[0];
        *(buf + index++) = string_buf[1];
    }
    *(buf + index++) = '\0';
    return buf;
}
uint8_t * format_time_inDay(uint8_t hour, uint8_t minute, uint8_t second, uint8_t *buf){
    uint8_t index = 0;
    uint8_t len = 0;
    len = myitoa(hour, string_buf, 10);
    if (len == 1) {
        *(buf + index++) = '0';
        *(buf + index++) = string_buf[0];
    } else {
        *(buf + index++) = string_buf[0];
        *(buf + index++) = string_buf[1];
    }
    *(buf + index++) = ':';
    len = myitoa(minute, string_buf, 10);
    if (len == 1) {
        *(buf + index++) = '0';
        *(buf + index++) = string_buf[0];
    } else {
        *(buf + index++) = string_buf[0];
        *(buf + index++) = string_buf[1];
    }
    *(buf + index++) = ':';
    len = myitoa(second, string_buf, 10);
    if (len == 1) {
        *(buf + index++) = '0';
        *(buf + index++) = string_buf[0];
    } else {
        *(buf + index++) = string_buf[0];
        *(buf + index++) = string_buf[1];
    }
    *(buf + index++) = '\0';
    return buf;
}
uint8_t *format_ID(uint16_t data, uint8_t *buff) {
    uint8_t len = myitoa(data, string_buf, 10);
    *(buff + 5) = '\0';
    switch (len) {
        case 1:
            *(buff + 4) = string_buf[0];
            *(buff + 3) = '0';
            *(buff + 2) = '0';
            *(buff + 1) = '0';
            *(buff + 0) = '0';
            break;
        case 2:
            *(buff + 4) = string_buf[1];
            *(buff + 3) = string_buf[0];
            *(buff + 2) = '0';
            *(buff + 1) = '0';
            *(buff + 0) = '0';
            break;
        case 3:
            *(buff + 4) = string_buf[2];
            *(buff + 3) = string_buf[1];
            *(buff + 2) = string_buf[0];
            *(buff + 1) = '0';
            *(buff + 0) = '0';
            break;
        case 4:
            *(buff + 4) = string_buf[3];
            *(buff + 3) = string_buf[2];
            *(buff + 2) = string_buf[1];
            *(buff + 1) = string_buf[0];
            *(buff + 0) = '0';
            break;
        case 5:
            *(buff + 4) = string_buf[4];
            *(buff + 3) = string_buf[3];
            *(buff + 2) = string_buf[2];
            *(buff + 1) = string_buf[1];
            *(buff + 0) = string_buf[0];
            break;
    }
    return buff;
}